//
//  SStringUtils.m
//  童伙妈妈—网络请求最新版
//
//  Created by mac on 16/7/5.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "SStringUtils.h"
#import <CommonCrypto/CommonDigest.h>

@implementation SStringUtils

+(NSString *)jsonStringWithParameters:(id)parameters{
    if (parameters == nil) {
        return @" ";
    }
    
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONReadingMutableContainers error:nil];
    NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    return string ;

    
}

+(NSString *)JSONString:(NSString *)aString {
    NSMutableString *s = [NSMutableString stringWithString:aString];
    [s replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"/" withString:@"\\/" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\n" withString:@"\\n" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\b" withString:@"\\b" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\f" withString:@"\\f" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\r" withString:@"\\r" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    [s replaceOccurrencesOfString:@"\t" withString:@"\\t" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s length])];
    return [NSString stringWithString:s];
}



+(BOOL)isTextEmpty:(NSString *)string{
    //判断字符串为空
    if (string == nil || (id)string == [NSNull null]) {
        return YES;
    }else{
        if (![string respondsToSelector:@selector(length)]) {
            return YES;
        }
        string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([string length]) {
            return NO;
        }
    }
    return YES;

}

+(NSString *)md5HexDigestWithString:(NSString *)str{
    const char *cStr = [str UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (int)str.length, digest );
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [result appendFormat:@"%02X", digest[i]];
    
    return result;
}

+(NSString *)MD5HexDigestWithString:(NSString *)str{
        const char *cStr = [str UTF8String];
        unsigned char result[16];
        NSNumber *num = [NSNumber numberWithUnsignedLong:strlen(cStr)];
        CC_MD5( cStr,[num intValue], result );
        return [[NSString stringWithFormat:
                 @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                 result[0], result[1], result[2], result[3],
                 result[4], result[5], result[6], result[7],
                 result[8], result[9], result[10], result[11],
                 result[12], result[13], result[14], result[15]
                 ] uppercaseString];
}


+(NSDictionary *)dataUsingEncoding:(NSString *)string{
    
    // 将JSON字符串解码为Foundation对象
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding]
                                                options:NSJSONReadingMutableContainers
                                                  error:&error];
    NSDictionary *dictionary;
    // 检查是否有错误发生
    if (error) {
        NSLog(@"解析错误: %@", error);
    } else {
        // 确认结果是一个字典
        
        
        
        if ([result isKindOfClass:[NSDictionary class]]) {
            dictionary = (NSDictionary *)result;

        } else {
            NSLog(@"解析结果不是一个字典");
        }
    }
    return result;
}

+(NSArray *)dataUsingEncodingByArr:(NSString *)str{
    
    NSError *error = nil;
    NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
     
    if (jsonArray) {
        // 成功解析JSON数组
        for (NSDictionary *person in jsonArray) {
            NSLog(@"Name: %@, Age: %@", person[@"name"], person[@"age"]);
        }
    } else {
        // 解析错误处理
        NSLog(@"解析错误: %@", error);
    }
    return jsonArray;
}

+(NSString *)stringUsingEncodingByArr:(NSArray *)array{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
    
    if (jsonData) {
        // 成功将数组转换为JSON数据
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return jsonString;
    } else {
        // 转换错误处理
        NSLog(@"转换错误: %@", error);
        return nil;
    }
}


@end





